package cn.intotw.rdcj.task.spiders.service.impl;

import cn.intotw.rdcj.task.bean.dto.BookContentBookQueryDto;
import cn.intotw.rdcj.task.bean.dto.BookListBookQueryDto;
import cn.intotw.rdcj.task.bean.dto.BookSectionsBookQueryDto;
import cn.intotw.rdcj.task.bean.vo.BookContentQueryVo;
import cn.intotw.rdcj.task.bean.vo.BookListQueryVo;
import cn.intotw.rdcj.task.bean.vo.BookSectionsQueryVo;
import cn.intotw.rdcj.task.spiders.client.NettyClientGroup;
import cn.intotw.rdcj.task.spiders.client.NettySpidersClient;
import cn.intotw.rdcj.task.spiders.netty.SettableTask;
import cn.intotw.rdcj.task.spiders.parser.BiqugeParser;
import cn.intotw.rdcj.task.spiders.service.BookService;
import cn.intotw.rdcj.task.spiders.util.HttpHeaderFactory;
import cn.intotw.rdcj.task.spiders.util.HttpRequestFactory;
import cn.intotw.rdcj.task.spiders.util.URLUtil;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.net.URI;
import java.net.URLEncoder;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;


/**
 * Created by Chenxiang
 *
 * @generator: IntelliJ IDEA
 * @description: 笔趣阁解析类
 * @project: rdcj
 * @package: cn.intotw.rdcj.task.spiders.service.impl
 * @date: 2020年07月27日 14时36分
 */
@Service("biqugeService")
@Slf4j
public class BiqugeServiceImpl implements BookService {

    private final URI uri= URLUtil.getUri("http://www.xbiquge.la/modules/article/waps.php");
    @Override
    public List<BookListQueryVo> queryBook(BookListBookQueryDto bookListQueryDto) throws Exception {
        String requestBody="searchkey="+ URLEncoder.encode(bookListQueryDto.getQueryKey(), "UTF-8");
        FullHttpRequest request = HttpRequestFactory.newFullPostHttpRequest(uri.getRawPath(), requestBody);
        request.headers().set(HttpHeaderFactory.getBiqugeHeaders(requestBody.length()));
        SettableTask<String> stringSettableTask = NettyClientGroup.getNettyClient(uri).sendAndSync(request);
        String s = stringSettableTask.get(5000, TimeUnit.MILLISECONDS);
        return BiqugeParser.parseBookList(s);
    }

    @Override
    public List<BookSectionsQueryVo> querySections(BookSectionsBookQueryDto bookSectionsQueryDto) throws Exception {
        String targetUrlPath = URLUtil.base64Decode(bookSectionsQueryDto.getTargetUrl());

        FullHttpRequest request = HttpRequestFactory.newFullHttpRequest(HttpMethod.GET,targetUrlPath, Unpooled.EMPTY_BUFFER);
        request.headers().set(HttpHeaderFactory.getBiqugeHeaders(0));
        SettableTask<String> stringSettableTask = NettyClientGroup.getNettyClient(uri).sendAndSync(request);
        String s = stringSettableTask.get(5000, TimeUnit.MILLISECONDS);
        log.info(s);
        return null;
    }

    @Override
    public BookContentQueryVo queryContent(BookContentBookQueryDto bookContentQueryDto) {
        return null;
    }
}
